//
//  WPMRTableViewCell.m
//  wobingwoyi
//
//  Created by WGP on 16/8/13.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPMRTableViewCell.h"
#import "WPMRModel.h"
#import "UIImage+AsyncDraw.h"
#import "YBPopupMenu.h"

#define imageScale 110/90.0
#define imageWidth (SCREEN_WIDTH-20-25)/3.0


@interface WPMRTableViewCell ()<YBPopupMenuDelegate>

@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UIImageView *editeImageView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIButton *chargeSharedBtn;
@property(nonatomic,strong)UIButton *sharedCoinBtn;
@property(nonatomic,strong)UILabel *stateLabel;
@property(nonatomic,strong)UIButton *shareBtn;
@property(nonatomic,strong)UILabel *describeLabel;
@property(nonatomic,strong)UIView *timeView;
@property(nonatomic,strong)UILabel *caseTimeLabel;
@property(nonatomic,strong)UIImageView *firstImageView;
@property(nonatomic,strong)UIImageView *secondImageView;
@property(nonatomic,strong)UIImageView *thirdImageView;

@property(nonatomic,strong)MASConstraint *describeLabelTopConstraint;
@property(nonatomic,strong)NSMutableArray *uploadImagesArr;

@end
@implementation WPMRTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = RGB_SAME(245);
        
        [self setupUI];
    }
    return  self;
}


-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setupUI];
    }
    return  self;
}

-(void)setupUI
{
    
//    1.添加控件
    self.bgView = [UIView new];
    self.bgView.backgroundColor  = [UIColor whiteColor];
    [self.contentView addSubview:self.bgView];
    
    UIImageView *topLine = [self lineImageView];
    UIImageView *bottomLine = [self lineImageView];
    [self.bgView addSubview:topLine];
    [self.bgView addSubview:bottomLine];
    
    self.editeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MR-bianji"]];
    [self.bgView addSubview:self.editeImageView];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    self.titleLabel.textColor = RGB_SAME(102);
    [self.titleLabel sizeToFit];
    [self.bgView addSubview:self.titleLabel];
    
    UIButton *chargeSharedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    chargeSharedBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [chargeSharedBtn setTitleColor:RGB_SAME(102) forState:UIControlStateNormal];
    [chargeSharedBtn setImage:[UIImage imageNamed:@"reading_coin"] forState:UIControlStateNormal];
    self.chargeSharedBtn = chargeSharedBtn;
    [chargeSharedBtn setTitle:@"  0" forState:UIControlStateNormal];
    [self.bgView addSubview:chargeSharedBtn];
//    self.sharedCoinBtn
    
    self.shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shareBtn setBackgroundImage:[UIImage imageNamed:@"case_share_normal"] forState:UIControlStateNormal];
    [self.shareBtn setBackgroundImage:[UIImage imageNamed:@"case_share_highlight"] forState:UIControlStateHighlighted];
    [self.shareBtn addTarget:self action:@selector(shareBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.shareBtn];
    
    self.stateLabel = [[UILabel alloc] init];
    self.stateLabel.font = [UIFont systemFontOfSize:13];
    self.stateLabel.textColor = RGB_SAME(102);
    [self.bgView addSubview:self.stateLabel];
    
    CGFloat imageW = imageWidth;
    CGFloat scale = 110/90.0;
    CGFloat imageH = imageW/scale;
    
    self.firstImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"banner"]];
    self.firstImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.firstImageView.clipsToBounds = YES;
    [self.bgView addSubview:self.firstImageView];
    
    self.secondImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"banner"]];
    self.secondImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.secondImageView.clipsToBounds = YES;
    [self.bgView addSubview:self.secondImageView];
    
    self.thirdImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"banner"]];
    self.thirdImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.thirdImageView.clipsToBounds = YES;
    [self.bgView addSubview:self.thirdImageView];
    
    self.describeLabel = [[UILabel alloc] init];
    self.describeLabel.textColor = RGB_SAME(51);
    self.describeLabel.font = [UIFont systemFontOfSize:13];
    self.describeLabel.numberOfLines = 2;
    [self.bgView addSubview:self.describeLabel];
    
    self.timeView = [UIView new];
    self.timeView.backgroundColor = [UIColor whiteColor];
    [self.bgView addSubview:self.timeView];
    UIImageView *caseTimeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"caseTimeImage"]];
    [self.timeView addSubview:caseTimeImageView];
    
    self.caseTimeLabel = [UILabel new];
    self.caseTimeLabel.font = [UIFont systemFontOfSize:14];
    self.caseTimeLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    [self.timeView addSubview:self.caseTimeLabel];
    
    UIImageView *lineView1 = [UIImageView lineImageView];
    UIImageView *lineView2 = [UIImageView lineImageView];
    [self.timeView addSubview:lineView1];
    [self.timeView addSubview:lineView2];
    
    
//    2.添加约束
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self.contentView);
        make.top.equalTo(self.contentView).offset(5);
        make.bottom.equalTo(self.contentView).offset(-5);
    }];
    
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.bgView);
        make.height.mas_equalTo(0.5);
    }];
    
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.bgView);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.editeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.equalTo(self.bgView).offset(10);
        make.width.height.mas_equalTo(15);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.editeImageView);
        make.left.equalTo(self.editeImageView.mas_right).offset(10);
    }];
    
    [self.chargeSharedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.editeImageView);
        make.left.equalTo(self.titleLabel.mas_right).offset(10);
    }];
    
    [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView).offset(-10);
        make.centerY.equalTo(self.titleLabel);
    }];
    
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel);
        make.right.equalTo(self.shareBtn.mas_left).offset(-10);
    }];
    
    [self.firstImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(imageWidth);
        make.height.mas_equalTo(imageH);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.left.equalTo(self.editeImageView);
    }];
    
    [self.secondImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerY.equalTo(self.firstImageView);
        make.left.equalTo(self.firstImageView.mas_right).offset(12.5);
    }];
    
    [self.thirdImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerY.equalTo(self.firstImageView);
        make.right.equalTo(self.bgView).offset(-10);
    }];
    
    [self.describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(10);
        make.right.equalTo(self.bgView).offset(-10);
        self.describeLabelTopConstraint = make.top.equalTo(self.firstImageView.mas_bottom).offset(15);
//        make.bottom.equalTo(self.bgView).offset(-20);
    }];
    [self.timeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.bgView);
        make.top.equalTo(self.describeLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(35);
    }];
    
    [caseTimeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeView).offset(10);
        make.centerY.equalTo(self.timeView);
    }];
    
    [self.caseTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.timeView);
        make.left.equalTo(caseTimeImageView.mas_right).offset(10);
    }];
    
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.timeView);
        make.height.mas_equalTo(0.5);
    }];
    
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.timeView);
        make.height.mas_equalTo(0.5);
    }];
}

-(void)setModel:(WPMRModel *)model
{
    _model = model;
    
    if (model.name.length)
    {
         self.titleLabel.text = model.name;
    }
    else
    {
        self.titleLabel.text = @"病例标题";
    }
    
    if (model.finalCureStatus.length)
    {
        self.stateLabel.text = model.finalCureStatus;
    }
    else
    {
        self.stateLabel.text = @"未治愈";
    }
    
    
    if(model.verified)
    {
        [self.shareBtn setBackgroundImage:[UIImage imageNamed:@"case_shared"] forState:UIControlStateNormal];
        [self.shareBtn setBackgroundImage:[UIImage imageNamed:@"shared_highlight"] forState:UIControlStateHighlighted];
        
        if (model.payable)
        {
            self.chargeSharedBtn.hidden = NO;
            [self.chargeSharedBtn setTitle:[NSString stringWithFormat:@"  %zd",model.pay] forState:UIControlStateNormal];
        }
        else
        {
            self.chargeSharedBtn.hidden = YES;
        }
    }
    else
    {
        self.chargeSharedBtn.hidden = YES;
        
        [self.shareBtn setBackgroundImage:[UIImage imageNamed:@"case_share_normal"] forState:UIControlStateNormal];
        [self.shareBtn setBackgroundImage:[UIImage imageNamed:@"case_share_highlight"] forState:UIControlStateHighlighted];
    }
    
//    设置图片
    if (self.model.firstOptionUrls.count == 0)
    {
        self.firstImageView.hidden = NO;
        self.secondImageView.hidden = YES;
        self.thirdImageView.hidden = YES;
        
        self.firstImageView.image = [UIImage imageNamed:@"noImages"];
    }
    else if (self.model.firstOptionUrls.count == 1)
    {
        self.firstImageView.hidden = NO;
        self.secondImageView.hidden = YES;
        self.thirdImageView.hidden = YES;
        [self.firstImageView sd_setImageWithURL:self.model.firstOptionUrls[0] placeholderImage:[UIImage imageNamed:@"placeholder_Image"]];
        
    }
    else if (self.model.firstOptionUrls.count == 2)
    {
        self.firstImageView.hidden = NO;
        self.secondImageView.hidden = NO;
        self.thirdImageView.hidden = YES;
        [self.firstImageView sd_setImageWithURL:self.model.firstOptionUrls[0] placeholderImage:[UIImage imageNamed:@"placeholder_Image"]];
        [self.secondImageView sd_setImageWithURL:self.model.firstOptionUrls[1] placeholderImage:[UIImage imageNamed:@"placeholder_Image"]];
        
    }
    else if (self.model.firstOptionUrls.count >= 3)
    {
        self.firstImageView.hidden = NO;
        self.secondImageView.hidden = NO;
        self.thirdImageView.hidden = NO;
        
        [self.firstImageView sd_setImageWithURL:self.model.firstOptionUrls[0] placeholderImage:[UIImage imageNamed:@"placeholder_Image"]];
        [self.secondImageView sd_setImageWithURL:self.model.firstOptionUrls[1] placeholderImage:[UIImage imageNamed:@"placeholder_Image"]];
        [self.thirdImageView sd_setImageWithURL:self.model.firstOptionUrls[2] placeholderImage:[UIImage imageNamed:@"placeholder_Image"]];
    }
    
    if (model.recordDescp.length)
    {
        
        [self.describeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            self.describeLabelTopConstraint =  make.top.equalTo(self.firstImageView.mas_bottom).offset(15);
        }];

        self.describeLabel.text = model.firstOptionStr;
    
    }
    else
    {
        self.describeLabel.text = @"暂无病例描述";
    }
    
    self.caseTimeLabel.text = [[WPDateFormatterManager sharedManager] timeStrWithTimeStampStr:[NSString stringWithFormat:@"%zd",model.uploadTime]];
}

-(void)shareBtnDidClick:(UIButton *)btn
{
    CGFloat width = [@"共享到普通病例" sizeWithMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) font:14].width;
    
    NSArray *titles = nil;
    if (_model.verified)
    {
         titles = @[@"取消共享"];
    }
    else
    {
         titles = @[@"共享到普通病例",@"共享到精华病例"];
    }
    
    YBPopupMenu *menu = [YBPopupMenu showRelyOnView:btn titles:titles icons:nil menuWidth:width + 35 delegate:self];
    menu.fontSize = 14;
    menu.textColor = GLobalFontColor;
}

#pragma mark - 弹出视图的代理方法

-(UIImageView *)lineImageView
{
    UIImageView *line = [[UIImageView alloc] init];
    line.backgroundColor = GLobalLineColor;
    return line;
}

- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu
{
    if(self.shareBtnDidClickBlock)
    {
        self.shareBtnDidClickBlock(index,self.model.verified,self.model);
    }
}

-(NSMutableArray *)uploadImagesArr
{
    if (_uploadImagesArr == nil)
    {
        _uploadImagesArr = [NSMutableArray array];
    }
    return _uploadImagesArr;
}
@end











